Repository navigation
feat(runtime): guard native methods against instance shadowing in debug builds (rooted/canary) - #593
Draft
edusperoni wants to merge 1 commit into
Draft
edusperoni wants to merge 1 commit into
edusperoni wants to merge 1 commit into
Conversation
…ng in debug builds
In debug builds under the "rooted" and "canary" wrapper lifetime modes,
assigning a native instance method's name on a native object throws a
TypeError: native code never calls a JS own property of a native object,
so the assignment only changes what JS callers see.
Native instance methods (protocol members included, and those of protocol
bindings), toString and NSArray's Symbol.iterator become accessor pairs on
their prototype once it is instantiated. The getter is a JS closure that
returns the original function, so reads, identity and super loads stay on
V8's fast paths. The setter throws for a native, released or proxied
native receiver and otherwise creates the own data property an assignment
would, so prototype-level writes still swizzle (once) and subclass
prototypes and objects inheriting from native ones are unaffected.
Object.defineProperty remains the route to a JS-only own member.
"wrapperLifetime": { "allowShadowingNativeMembers": true } turns the guard
off; ns:runtime exposes it read-only as
wrapperLifetimeAllowShadowingNativeMembers. Release builds and the
resurrecting mode keep data properties.
|
Important Draft PR not reviewedDraft PRs are not automatically reviewed by default.
To automatically review draft PRs, update your CodeRabbit configuration: reviews:
auto_review:
drafts: true
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. |
This branch has not been deployed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
In the
rootedandcanarywrapper lifetime modes, a JS wrapper can die while its native object lives. In canary mode, a wrapper is rebuilt when native code hands the object back. Assigning a native method's name on an instance (obj.isEqual = fn) was always JS-only: native code calls the class's implementation, never a JS own property of a native object. In these modes the shadow can also be lost silently when the wrapper is rebuilt. This PR makes that assignment throw in debug builds, so the mistake shows up during development.Behavior
Debug builds (
RuntimeConfig.IsDebug) withwrapperLifetime.mode"rooted"or"canary", any scope, unless"allowShadowingNativeMembers": true:toString, and NSArray'sSymbol.iterator.obj.m === obj.m,typeof,.call/.bind,in,super.m()and_super.prototype.m.call(this)are unchanged.TypeErrorwhen the receiver is a native object wrapper, a released one, or a Proxy around one. The message names the method, the class, the mode and the escape hatch. Any other receiver gets the own data property a plain assignment would create. That keeps__extends/@NativeClasschild prototypes,Object.create(wrapper)and extend() overrides working.Klass.prototype.m = fnstill swizzles once. That name then becomes a plain data property and is no longer guarded.constructor;Object.defineProperty, which is the documented escape route.Object.getOwnPropertyDescriptor(UIView.prototype, "setNeedsLayout")is{ get, set, enumerable, configurable }."resurrecting"keep plain data properties. On an iPhone 11 Pro (jitless), a guard accessor costs +11 to +15 ns per method load. An API (C++) getter was also measured and rejected: V8 sendssuper.mloads through an API getter to the slow path (about +100 ns), while the JS closure getter adds about +8 ns there.getConfig("wrapperLifetimeAllowShadowingNativeMembers")reads the new key; it is read-only at runtime, like the other wrapper lifetime keys.Implementation
MetadataBuildercollects the method names it registers. After the prototype is instantiated, beforePublishNativeSetters, it swaps each own data function for the accessor pair. The getter comes from a JS factory compiled once per isolate. The setter is one C++ function whose data is the property name.WrapperLifetime::ReadConfigcomputes one process-wide flag: debug, guarded mode and no escape hatch.Specs
NativeMemberGuardTests.js(new, withTNSNativeMemberGuardfixtures). It checks:toStringandSymbol.iterator;call/bind/in;__extends, extend() overrides with native dispatch,superand_super;Object.createanddefineProperty;"resurrecting"or with the escape hatch.WrapperLifetimeModesTests.js: the spec that shadowedisEqualon an instance now expects theTypeErrorin guarded modes.Testing
2173 specs in each run:
canary/allcanary/extendedresurrecting/extendedcanary/all+allowShadowingNativeMemberscanary/allrooted/allThe
rooted/allfailure was in the leak-probe "planted shapes" specs, in 2 of 5 runs. Four later runs were clean, with the guard on and off. It looks timing-dependent and unrelated to the guard; #591 reworks that area.